package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionDao permissionDao;

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        if(queryString!=null){
            currentPage=1;
        }
        //第一步：设置分页参数
        PageHelper.startPage(currentPage,pageSize);
        //第二步：查询数据库
        Page<Permission> permissionPage=permissionDao.selectByCondition(queryString);
        return new PageResult(permissionPage.getTotal(),permissionPage.getResult());
    }

    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /**
     * 根据id查询权限数据
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    /**
     * 根据id更新权限数据
     * @param permission
     */
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    /**
     * 查询此权限是否被用户使用
     * @param id
     * @return
     */
    @Override
    public Result findCountByPermissionId(Integer id) {
        StringBuffer stringBuffer = new StringBuffer();
        List<Role> roles=new ArrayList();
        roles= permissionDao.findCountByPermissionId(id);
        if(roles.size()>0){
            for (Role role : roles) {
                stringBuffer.append(role.getName()+" ");
            }
            return new Result(false,("已被"+stringBuffer.toString()+"使用"),roles);
        }else {
            return new Result(true,"未被使用");
        }
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        try {
            //删除角色和权限连接表的数据
            permissionDao.deleteRole_PermissionByPermissionId(id);
            //删除权限
            permissionDao.deletePermissionByPermissionId(id);
            return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    @Override
    public Result findCountByPermissionName(String name) {
        if (permissionDao.findCountByPermissionName(name) > 0) {
            return new Result(false, "权限名称重复", permissionDao.findCountByPermissionName(name));
        }else {
            return new Result(true, "权限名称可以使用", permissionDao.findCountByPermissionName(name));
        }
    }

    /**
     * 查询所有权限列表
     * @return
     */
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }


}
